package packet1;

public class island {
    public static void main(String[] args) {
        System.out.println(land(new char[][]{{'1','1','0','0'},{'0','0','1','0'}}));
    }
    static int  land(char[][] grid){
        int sum = 0;
        for (int i = 0; i < grid.length; i++) {
            for (int j = 0; j < grid[0].length; j++) {
                if(grid[i][j] == '1')
                {
                    sum++;
                    dfs(grid, i ,j);
                }

            }
        }
        return sum;
    }
    static void dfs(char[][] grid, int i, int j)
    {
        if(i < 0 || i >= grid.length || j < 0 || j >= grid[0].length || grid[i][j] == '2' || grid[i][j] == '0')
            return;
        grid[i][j] = '2';
        dfs(grid, i - 1, j);
        dfs(grid, i + 1, j);
        dfs(grid, i, j - 1);
        dfs(grid, i, j + 1);
    }

}
